﻿/*
    Create by: son.nguyen
    Date: 25.03.2010
*/

// =========================== Check All Grid ===============================//
function SelectAllItemByCheckBox(idgrid,id) {    
    var grid = document.getElementById(idgrid);
    if (grid.rows.length > 0) {        
        for (i = 1; i < grid.rows.length; i++) {
            var cell = grid.rows[i].cells[0];        
            for (j = 0; j < cell.childNodes.length; j++) {                
                if (cell.childNodes[j].type == "checkbox") {                    
                    cell.childNodes[j].checked = document.getElementById(id).checked;
                }
            }
        }//end for
    }
}
//check item delete
function Delete(idgrid) {
    var grid = document.getElementById(idgrid);
    var flag = false;
    if (grid.rows.length > 0) {
        for (i = 1; i < grid.rows.length; i++) {
            var cell = grid.rows[i].cells[0];
            for (j = 0; j < cell.childNodes.length; j++) {
                if (cell.childNodes[j].type == "checkbox" && cell.childNodes[j].checked) {
                    flag = true;
                }
            }
        } //end for
    }
    if (flag) {
        return confirm("Có chắc muốn xóa dữ liệu này không?");
    }
    else {
        alert("Chưa có dòng dữ liệu nào được chọn.");
        return false;
    }
}
//check item edit
function Edit(idgrid) {
    var grid = document.getElementById(idgrid);
    var flag = false;
    if (grid.rows.length > 0) {
        for (i = 1; i < grid.rows.length; i++) {
            var cell = grid.rows[i].cells[0];
            for (j = 0; j < cell.childNodes.length; j++) {
                if (cell.childNodes[j].type == "checkbox" && cell.childNodes[j].checked) {
                    flag = true;
                }
            }
        } //end for
    }
    if (flag) {
        return true;
    }
    else {
        alert("Chưa có dòng dữ liệu nào được chọn.");
        return false;
    }
}
//check select item Grid
function CheckSelectItemGrid(idgrid) {
    var grid = document.getElementById(idgrid);
    var flag = false;
    if (grid.rows.length > 0) {
        for (i = 1; i < grid.rows.length; i++) {
            var cell = grid.rows[i].cells[0];
            for (j = 0; j < cell.childNodes.length; j++) {
                if (cell.childNodes[j].type == "checkbox" && cell.childNodes[j].checked) {
                    flag = true;
                }
            }
        } //end for
    }
    if (flag) {
        return true;
    }
    else {
        alert("Chưa có dòng dữ liệu nào được chọn.");
        return false;
    }
}

// =========================== ham lay gia tri cua tham so tren url =========================== //
function getPramJs(param) {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i = 0; i < parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0, pos).toLowerCase();
            var val = parms[i].substring(pos + 1);
            if (key == param.toLowerCase())
                return val;
        }
    }
    return null;
}
//ham lay dia chi url hien tai
function getUrlLocal() {
    var requestURL = window.document.location.toString();
    var end = requestURL.lastIndexOf("/") + 1;
    requestURL = requestURL.substring(0, end);
    return requestURL;
}

// =========================== Format number =========================== //
// Created: son.nguyen
// Date:    26/03/2010
function onkeypressnumber(e, dec) {
    var regex = /[0-9]|\./;    
    var key, keychar;
    if (window.event) { key = window.event.keyCode ? window.event.keyCode : e.which; }
    else { key = e.which; }
    if (!dec) regex = /[0-9]/;
    keychar = String.fromCharCode(key);
    // control keys
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27))
        return true;
    else if (regex.test(keychar)) {        
        return true;
    }
    else {
        return false;
    }
}
// Created: son.nguyen
// Date:    26/03/2010
function extractValidationNumbers(obj, decimalPlaces, allowNegative) {
    var temp = obj.value;
    // avoid changing things if already formatted correctly
    var reg0Str = '[0-9]*';
    if (decimalPlaces > 0) {
        reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
    } else if (decimalPlaces < 0) {
        reg0Str += '\\.?[0-9]*';
    }
    reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
    reg0Str = reg0Str + '$';
    var reg0 = new RegExp(reg0Str);
    if (reg0.test(temp)) {
        if (allowNegative && temp.length > 0 && temp.charAt(0) == '-') {
            temp = addCommas(temp.substring(1), '-');
        }
        else {
            temp = addCommas(temp, '');
        }
        obj.value = temp;
        return true;
    }
    // first replace all non numbers
    var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
    var reg1 = new RegExp(reg1Str, 'g');
    temp = temp.replace(reg1, '');
    if (allowNegative) {
        // replace extract negative
        var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
        var reg2 = /-/g;
        temp = temp.replace(reg2, '');
        if (hasNegative) temp = '-' + temp;
    }
    if (decimalPlaces != 0) {
        var reg3 = /\./g;
        var reg3Array = reg3.exec(temp);
        if (reg3Array != null) {
            // keep only first occurrence of . 
            //  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
            var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
            reg3Right = reg3Right.replace(reg3, '');
            reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
            temp = temp.substring(0, reg3Array.index) + '.' + reg3Right;
        }
    }
    //format number
    if (allowNegative && temp.length > 0 && temp.charAt(0) == '-') {
        temp = addCommas(temp.substring(1), '-');
    }
    else {
        temp = addCommas(temp, '');
    }   
    obj.value = temp;
}
// Created: son.nguyen
// Date:    26/03/2010
function inputValidationNumbers(obj, e, allowDecimal, allowNegative) {
    var key;
    var isCtrl = false;
    var keychar;
    var reg;
    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey
        if (e.keyCode == 0 && window.event.which == 1) {
            key = e.which;
            isCtrl = e.ctrlKey;
        }
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
    }
    if (isNaN(key)) return true;
    keychar = String.fromCharCode(key);
    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8 || isCtrl) {
        return true;
    }
    reg = /\d/;
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;

    return isFirstN || isFirstD || reg.test(keychar);
}
// =========================== Format Date =========================== //
// Created: son.nguyen
// Date:    26/03/2010
function addCommas(nStr, prefix) {
    var prefix = prefix || '';
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    return prefix + x1 + x2;
}
// =========================== Format Date =========================== //
// Created: son.nguyen
// Date:    26/03/2010
function inputValidationDate(e) {
    var key;
    var isCtrl = false;
    var keychar;
    var reg;
    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey      
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
    }
    if (isNaN(key)) return true;
    keychar = String.fromCharCode(key);
    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8 || isCtrl) {
        return true;
    }
    reg = /\d|\/$/;

    return reg.test(keychar);
}
// Created: son.nguyen
// Date:    26/03/2010
function extractValidationDate(obj) {
    var temp = obj.value;
    // avoid changing things if already formatted correctly
    var reg0Str = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;     
    var reg0 = new RegExp(reg0Str);
    if (reg0.test(temp)) return true;
    obj.value = temp;
} 
